home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15475 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  88 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory leakage
  5. Date: 19 Apr 1996 07:17:46 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4l7emq$a2n@sparcserver.lrz-muenchen.de>
  9. References: <4l3582$1v@alice.walrus.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. warrenj@walrus.com (Warren Johnson) writes:
  13.  
  14. >Suppose I have a function:
  15.  
  16. >int give_number() {
  17. >   ...
  18. >   ...
  19. >   ...
  20. >}
  21.  
  22. >and in main I have a line :
  23.  
  24. >if(x>give_number()) { 
  25. >  ...
  26. >  ... }
  27.  
  28. >Now my question is this:  give_number() returns an integer.  Obviously 
  29. >the compiler must allocate memory for this integer.  However, we are not 
  30. >returning this integer to an integer pointer, therefore this allocated 
  31. >memory is floating about in the void somewhere without something pointing 
  32. >to it.
  33.  
  34. Consider evaluating this expression:
  35.  
  36. int x, y = 42;
  37.  
  38. x = 3 * y + 10;
  39.  
  40. "3 * y" is evaluated first, the result is stored in a temporary variable
  41. that you will never see (well, in most implementations it will be stored
  42. in a register anyway), then 10 is added, and the result is stored in 
  43. either the same or an other temporary variable that you will never see.
  44.  
  45. >Is this memory deallocated when the function is finished running 
  46. >it's course or do i have to do this:
  47.  
  48. >y = give_number();
  49.  
  50. >if(x>y) { blabalh}
  51.  
  52. >free(y); // or just let y be deallocated when the function is over with
  53.  
  54. This is a syntax error in C as it is defined today, but it may be a 
  55. very dangerous expression if "//" will be accepted as a comment delimiter
  56. in a future version of the language definition.
  57.  
  58. _Never_ pass something to free that is not a pointer value returned from
  59. a memory allocation function, i.e. from either malloc(),  calloc() or
  60. realloc().
  61.  
  62. You don't care about "deallocating" temporary variables that are used
  63. in the evaluation of "normal" expressions, so there is no need to
  64. care about temporary variables that are used to store the return 
  65. value of a function.
  66.  
  67. If your function returns a pointer to memory allocated in that function,
  68. you will have to think about freeing it if you do not need it any more.
  69.  
  70.    char *copyName(const char *name)
  71.    {
  72.       char *res;
  73.  
  74.       res = malloc(strlen(name) + 1);
  75.       if (res)
  76.          strcpy(res, name);
  77.       return res;
  78.    }
  79.  
  80. In this case, the _caller_ of that function is responsible for 
  81. deallocating the memory allocated in the function, but this is
  82. because the memory was _allocated in the function_.
  83.  
  84. Kurt
  85. --
  86. | Kurt Watzka                             Phone : +49-89-2180-6254
  87. | watzka@stat.uni-muenchen.de
  88.